package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckgroupDTO;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.pojo.Checkgroup;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckgroupService;
import com.itheima.service.SetmealGroupService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.utils.RedisUtil;
import com.itheima.vo.CheckGroupVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@DubboService
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, Checkgroup> implements CheckgroupService {
    @Autowired
    private CheckGroupItemService checkGroupItemService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealGroupService setmealGroupService;

    @Override
    public List<CheckGroupVo> findGroupsBysId(Long sId) {
       /* if(RedisUtil.get("checkgroup:setmealId:"+sId)!=null){
            return RedisUtil.get("checkgroup:setmealId:"+sId);
        }
        RedisUtil.set("checkgroup:setmealId:"+sId,baseMapper.findGroupsBysId(sId));*/
        return baseMapper.findGroupsBysId(sId);
    }

    @Override
    @Transactional
    public boolean saveGroup(CheckgroupDTO checkgroupDTO) {
        Long[] itemIds = checkgroupDTO.getItemIds();
        Checkgroup checkgroup = checkgroupDTO.getCheckgroup();
        saveOrUpdate(checkgroup);
        QueryWrapper<CheckGroupItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id",checkgroup.getId());
        checkGroupItemService.remove(queryWrapper);
        List<CheckGroupItem> list = new ArrayList<>();
        for (Long itemId : itemIds) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setGroupId(checkgroup.getId());
            checkGroupItem.setItemId(itemId);
            list.add(checkGroupItem);
        }
        Long[] setmealByGid = setmealGroupService.findSetmealByGid(checkgroupDTO.getCheckgroup().getId());
        for (Long sid : setmealByGid) {
            redisTemplate.delete(redisTemplate.keys("setmeal:"+sid));
        }
        checkGroupItemService.saveBatch(list);
        return true;
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Checkgroup> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!ObjectUtils.isEmpty(queryString)) {
            queryWrapper.and(wrapper -> {
                wrapper.like("code", queryString).or()
                        .like("name", queryString).or()
                        .like("help_code", queryString);
            });
        }

        Page<Checkgroup> page = page(new Page<Checkgroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }
}
